<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>zbus消息存储机制 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="背景　　目前消息队列产品，如rabbtimq、activemq和zbus等广泛用于各大业务系统，为避免消息队列系统崩溃时，出现消息丢失的情况，需要对消息进行持久化操作，以保证系统的可靠运行！既然要对消息进行持久化操作，那么其实现机制是什么呢？如何保证高效稳定？下面我们看看oschina上的开源项目zbus的实现机制，理解其核心原理！后来发现，zbus的作者在源码中指出其持久化实现原理参考了用map">
<meta property="og:type" content="article">
<meta property="og:title" content="zbus消息存储机制">
<meta property="og:url" content="http://yoursite.com/2016/01/15/java/11.zbus消息存储机制/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="背景　　目前消息队列产品，如rabbtimq、activemq和zbus等广泛用于各大业务系统，为避免消息队列系统崩溃时，出现消息丢失的情况，需要对消息进行持久化操作，以保证系统的可靠运行！既然要对消息进行持久化操作，那么其实现机制是什么呢？如何保证高效稳定？下面我们看看oschina上的开源项目zbus的实现机制，理解其核心原理！后来发现，zbus的作者在源码中指出其持久化实现原理参考了用map">
<meta property="og:image" content="http://yoursite.com/images/zbus-queue-persist.png">
<meta property="og:updated_time" content="2016-01-29T17:47:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="zbus消息存储机制">
<meta name="twitter:description" content="背景　　目前消息队列产品，如rabbtimq、activemq和zbus等广泛用于各大业务系统，为避免消息队列系统崩溃时，出现消息丢失的情况，需要对消息进行持久化操作，以保证系统的可靠运行！既然要对消息进行持久化操作，那么其实现机制是什么呢？如何保证高效稳定？下面我们看看oschina上的开源项目zbus的实现机制，理解其核心原理！后来发现，zbus的作者在源码中指出其持久化实现原理参考了用map">
  
    <link rel="alternative" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  

</head>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-java/11.zbus消息存储机制" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2016/01/15/java/11.zbus消息存储机制/" class="article-date">
  <time datetime="2016-01-15T06:09:00.000Z" itemprop="datePublished">2016-01-15</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/java/">java</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      zbus消息存储机制
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u80CC_u666F"><a href="#u80CC_u666F" class="headerlink" title="背景"></a>背景</h2><p>　　目前消息队列产品，如rabbtimq、activemq和zbus等广泛用于各大业务系统，为避免消息队列系统崩溃时，出现消息丢失的情况，需要对消息进行持久化操作，以保证系统的可靠运行！既然要对消息进行持久化操作，那么其实现机制是什么呢？如何保证高效稳定？下面我们看看oschina上的开源项目zbus的实现机制，理解其核心原理！后来发现，zbus的作者在源码中指出其持久化实现原理参考了<a href="http://my.oschina.net/xnkl/blog/477690?fromerr=Gr7v5nwR" target="_blank" rel="external">用mappedbytebuffer实现一个持久化队列</a>。也与高性能队列Fqueue的实现类似。下面我们简单说说它的机制！</p>
<h2 id="u601D_u8DEF"><a href="#u601D_u8DEF" class="headerlink" title="思路"></a>思路</h2><p>　　我们先看看网上的一张图：<img src="/images/zbus-queue-persist.png" alt="zbus-queue-persist.png"><br>　　上图就是队列的文件系统，它包含两个部分：索引(index)文件和数据(block)文件。index文件记录了队列当前的读写文件号，读写位置和读写计数。队列的size是通过读写计数writeCounter-readCounter的方式记录的，这样做的好处是可以做到读写分离。运行时size用一个AtomicInteger变量记录，系统初始化加载队列时才用到读写计数差。<br>　　block（数据）文件记录了实际的入队数据，每个block必须要有足够的空间写入4(len)+data.length+4(EOF)长度的数据，否则写入一个EOF标记，换一个新的block开始写入数据，而当读取到这个EOF时，表示这个block读取完毕，载入下一个block，如果有的话，释放并准备删除当前block。默认规定的block大小32MB。</p>
<h2 id="u5B9E_u73B0"><a href="#u5B9E_u73B0" class="headerlink" title="实现"></a>实现</h2><p>　　在zbus中负责队列持久化的类为：org.zbus.mq.server.support.DiskQueuePool。对照上图，我们在DiskQueuePool可以找到索引文件和数据文件对应的内部类，分别为：DiskQueueIndex和DiskQueueBlock。我们先看看这两个内部类的实现</p>
<h2 id="DiskQueueIndex"><a href="#DiskQueueIndex" class="headerlink" title="DiskQueueIndex"></a>DiskQueueIndex</h2><p>　　我们先看看DiskQueueIndex的关键代码，可以发现，作者通过两个MappedByteBuffer类型的writeIndex和readIndex，实现了索引文件的读写分析。每次初始化DiskQueueIndex时，都要读取该索引文件，获取数据文件和消息的读写位置等元数据信息。如果没有，则创建索引文件。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br></pre></td><td class="code"><pre><span class="line">  <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">DiskQueueIndex</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String MAGIC = <span class="string">"v100"</span>;</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String INDEX_FILE_SUFFIX = <span class="string">".idx"</span>;</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> INDEX_SIZE = <span class="number">32</span>;</span><br><span class="line">      </span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> FLAG_OFFSET = <span class="number">4</span>;</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> READ_NUM_OFFSET = <span class="number">8</span>;</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> READ_POS_OFFSET = <span class="number">12</span>;</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> READ_CNT_OFFSET = <span class="number">16</span>;</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> WRITE_NUM_OFFSET = <span class="number">20</span>;</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> WRITE_POS_OFFSET = <span class="number">24</span>;</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> WRITE_CNT_OFFSET = <span class="number">28</span>;</span><br><span class="line"> </span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">int</span> flag;</span><br><span class="line">      </span><br><span class="line">      <span class="annotation">@SuppressWarnings</span>(<span class="string">"unused"</span>)</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> p11, p12, p13, p14, p15, p16, p17, p18; <span class="comment">// 缓存行填充 32B</span></span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">int</span> readPosition;   <span class="comment">// 12   读索引位置</span></span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">int</span> readNum;        <span class="comment">// 8   读索引文件号</span></span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">int</span> readCounter;    <span class="comment">// 16   总读取数量</span></span><br><span class="line">      </span><br><span class="line">      <span class="annotation">@SuppressWarnings</span>(<span class="string">"unused"</span>)</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> p21, p22, p23, p24, p25, p26, p27, p28; <span class="comment">// 缓存行填充 32B</span></span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">int</span> writePosition;  <span class="comment">// 24  写索引位置</span></span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">int</span> writeNum;       <span class="comment">// 20  写索引文件号</span></span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">int</span> writeCounter;   <span class="comment">// 28 总写入数量</span></span><br><span class="line">  </span><br><span class="line">      <span class="annotation">@SuppressWarnings</span>(<span class="string">"unused"</span>)</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> p31, p32, p33, p34, p35, p36, p37, p38; <span class="comment">// 缓存行填充 32B</span></span><br><span class="line"> </span><br><span class="line">      <span class="keyword">private</span> RandomAccessFile indexFile;</span><br><span class="line">      <span class="keyword">private</span> FileChannel fileChannel;</span><br><span class="line">      <span class="comment">// 读写分离</span></span><br><span class="line">      <span class="keyword">private</span> MappedByteBuffer writeIndex;</span><br><span class="line">      <span class="keyword">private</span> MappedByteBuffer readIndex;</span><br><span class="line">       </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="title">DiskQueueIndex</span><span class="params">(String indexFilePath)</span> </span>&#123; </span><br><span class="line">          File file = <span class="keyword">new</span> File(indexFilePath);</span><br><span class="line">          <span class="keyword">try</span> &#123;</span><br><span class="line">              <span class="keyword">if</span> (file.exists()) &#123;</span><br><span class="line">                  <span class="keyword">this</span>.indexFile = <span class="keyword">new</span> RandomAccessFile(file, <span class="string">"rw"</span>);</span><br><span class="line">                  <span class="keyword">byte</span>[] bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">4</span>];</span><br><span class="line">                  <span class="keyword">this</span>.indexFile.read(bytes, <span class="number">0</span>, <span class="number">4</span>);</span><br><span class="line">                  <span class="keyword">if</span> (!MAGIC.equals(<span class="keyword">new</span> String(bytes))) &#123;</span><br><span class="line">                      <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"version mismatch"</span>);</span><br><span class="line">                  &#125;</span><br><span class="line">                  <span class="keyword">this</span>.flag = indexFile.readInt();</span><br><span class="line">                  <span class="keyword">this</span>.readNum = indexFile.readInt();</span><br><span class="line">                  <span class="keyword">this</span>.readPosition = indexFile.readInt();</span><br><span class="line">                  <span class="keyword">this</span>.readCounter = indexFile.readInt();</span><br><span class="line">                  <span class="keyword">this</span>.writeNum = indexFile.readInt();</span><br><span class="line">                  <span class="keyword">this</span>.writePosition = indexFile.readInt();</span><br><span class="line">                  <span class="keyword">this</span>.writeCounter = indexFile.readInt();</span><br><span class="line">                  </span><br><span class="line">                  <span class="keyword">this</span>.fileChannel = indexFile.getChannel();</span><br><span class="line">                  <span class="keyword">this</span>.writeIndex = fileChannel.map(FileChannel.MapMode.READ_WRITE, <span class="number">0</span>, INDEX_SIZE);</span><br><span class="line">                  <span class="keyword">this</span>.writeIndex = writeIndex.load();</span><br><span class="line">                  <span class="keyword">this</span>.readIndex = (MappedByteBuffer) writeIndex.duplicate();</span><br><span class="line">              &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                  <span class="keyword">this</span>.indexFile = <span class="keyword">new</span> RandomAccessFile(file, <span class="string">"rw"</span>);</span><br><span class="line">                  <span class="keyword">this</span>.fileChannel = indexFile.getChannel();</span><br><span class="line">                  <span class="keyword">this</span>.writeIndex = fileChannel.map(FileChannel.MapMode.READ_WRITE, <span class="number">0</span>, INDEX_SIZE);</span><br><span class="line">                  <span class="keyword">this</span>.readIndex = (MappedByteBuffer) writeIndex.duplicate();</span><br><span class="line">                  putMagic();</span><br><span class="line">                  putFlag(<span class="number">0</span>);</span><br><span class="line">                  putReadNum(<span class="number">0</span>);</span><br><span class="line">                  putReadPosition(<span class="number">0</span>);</span><br><span class="line">                  putReadCounter(<span class="number">0</span>);</span><br><span class="line">                  putWriteNum(<span class="number">0</span>);</span><br><span class="line">                  putWritePosition(<span class="number">0</span>);</span><br><span class="line">                  putWriteCounter(<span class="number">0</span>);</span><br><span class="line">              &#125;</span><br><span class="line">          &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">              <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(e);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isIndexFile</span><span class="params">(String fileName)</span> </span>&#123;</span><br><span class="line">          <span class="keyword">return</span> fileName.endsWith(INDEX_FILE_SUFFIX);</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">parseQueueName</span><span class="params">(String indexFileName)</span> </span>&#123;</span><br><span class="line">          String fileName = indexFileName.substring(<span class="number">0</span>, indexFileName.lastIndexOf(<span class="string">'.'</span>));</span><br><span class="line">          <span class="keyword">return</span> fileName;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">formatIndexFilePath</span><span class="params">(String queueName, String fileBackupDir)</span> </span>&#123;</span><br><span class="line">          <span class="keyword">return</span> fileBackupDir + File.separator + String.format(<span class="string">"%s%s"</span>, queueName, INDEX_FILE_SUFFIX);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">//省略属性对应的get方法</span></span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">putMagic</span><span class="params">()</span> </span>&#123;</span><br><span class="line">          <span class="keyword">this</span>.writeIndex.position(<span class="number">0</span>);</span><br><span class="line">          <span class="keyword">this</span>.writeIndex.put(MAGIC.getBytes());</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">putFlag</span><span class="params">(<span class="keyword">int</span> flag)</span></span>&#123;</span><br><span class="line">      	<span class="keyword">this</span>.writeIndex.position(FLAG_OFFSET);</span><br><span class="line">      	<span class="keyword">this</span>.writeIndex.putInt(flag);</span><br><span class="line">      	<span class="keyword">this</span>.flag = flag;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">putWritePosition</span><span class="params">(<span class="keyword">int</span> writePosition)</span> </span>&#123;</span><br><span class="line">          <span class="keyword">this</span>.writeIndex.position(WRITE_POS_OFFSET);</span><br><span class="line">          <span class="keyword">this</span>.writeIndex.putInt(writePosition);</span><br><span class="line">          <span class="keyword">this</span>.writePosition = writePosition;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">putWriteNum</span><span class="params">(<span class="keyword">int</span> writeNum)</span> </span>&#123;</span><br><span class="line">          <span class="keyword">this</span>.writeIndex.position(WRITE_NUM_OFFSET);</span><br><span class="line">          <span class="keyword">this</span>.writeIndex.putInt(writeNum);</span><br><span class="line">          <span class="keyword">this</span>.writeNum = writeNum;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">putWriteCounter</span><span class="params">(<span class="keyword">int</span> writeCounter)</span> </span>&#123;</span><br><span class="line">          <span class="keyword">this</span>.writeIndex.position(WRITE_CNT_OFFSET);</span><br><span class="line">          <span class="keyword">this</span>.writeIndex.putInt(writeCounter);</span><br><span class="line">          <span class="keyword">this</span>.writeCounter = writeCounter;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">putReadNum</span><span class="params">(<span class="keyword">int</span> readNum)</span> </span>&#123;</span><br><span class="line">          <span class="keyword">this</span>.readIndex.position(READ_NUM_OFFSET);</span><br><span class="line">          <span class="keyword">this</span>.readIndex.putInt(readNum);</span><br><span class="line">          <span class="keyword">this</span>.readNum = readNum;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">putReadPosition</span><span class="params">(<span class="keyword">int</span> readPosition)</span> </span>&#123;</span><br><span class="line">          <span class="keyword">this</span>.readIndex.position(READ_POS_OFFSET);</span><br><span class="line">          <span class="keyword">this</span>.readIndex.putInt(readPosition);</span><br><span class="line">          <span class="keyword">this</span>.readPosition = readPosition;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">putReadCounter</span><span class="params">(<span class="keyword">int</span> readCounter)</span> </span>&#123;</span><br><span class="line">          <span class="keyword">this</span>.readIndex.position(READ_CNT_OFFSET);</span><br><span class="line">          <span class="keyword">this</span>.readIndex.putInt(readCounter);</span><br><span class="line">          <span class="keyword">this</span>.readCounter = readCounter;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">reset</span><span class="params">()</span> </span>&#123;</span><br><span class="line">          <span class="keyword">int</span> size = writeCounter - readCounter;</span><br><span class="line">          putReadCounter(<span class="number">0</span>);</span><br><span class="line">          putWriteCounter(size);</span><br><span class="line">          <span class="keyword">if</span> (size == <span class="number">0</span> &amp;&amp; readNum == writeNum) &#123;</span><br><span class="line">              putReadPosition(<span class="number">0</span>);</span><br><span class="line">              putWritePosition(<span class="number">0</span>);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sync</span><span class="params">()</span> </span>&#123;</span><br><span class="line">          <span class="keyword">if</span> (writeIndex != <span class="keyword">null</span>) &#123;</span><br><span class="line">              writeIndex.force();</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">close</span><span class="params">()</span> </span>&#123;</span><br><span class="line">          <span class="keyword">try</span> &#123;</span><br><span class="line">              <span class="keyword">if</span> (writeIndex == <span class="keyword">null</span>) &#123;</span><br><span class="line">                  <span class="keyword">return</span>;</span><br><span class="line">              &#125;</span><br><span class="line">              sync();</span><br><span class="line">              AccessController.doPrivileged(<span class="keyword">new</span> PrivilegedAction&lt;Object&gt;() &#123;</span><br><span class="line">              	<span class="annotation">@SuppressWarnings</span>(<span class="string">"restriction"</span>)</span><br><span class="line">                  <span class="function"><span class="keyword">public</span> Object <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                      <span class="keyword">try</span> &#123;</span><br><span class="line">                          Method getCleanerMethod = writeIndex.getClass().getMethod(<span class="string">"cleaner"</span>);</span><br><span class="line">                          getCleanerMethod.setAccessible(<span class="keyword">true</span>); </span><br><span class="line">					sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(writeIndex);</span><br><span class="line">                          cleaner.clean();</span><br><span class="line">                      &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                          log.error(<span class="string">"close fqueue index file failed"</span>, e);</span><br><span class="line">                      &#125;</span><br><span class="line">                      <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">                  &#125;</span><br><span class="line">              &#125;);</span><br><span class="line">              writeIndex = <span class="keyword">null</span>;</span><br><span class="line">              readIndex = <span class="keyword">null</span>;</span><br><span class="line">              fileChannel.close();</span><br><span class="line">              indexFile.close();</span><br><span class="line">          &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">              log.error(<span class="string">"close fqueue index file failed"</span>, e);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="DiskQueueBlock"><a href="#DiskQueueBlock" class="headerlink" title="DiskQueueBlock"></a>DiskQueueBlock</h2><p>　　下面我们看看DiskQueueBlock的关键代码。可以发现，文件的结束标志EOF取值为-1。每次通过write和read方法读写消息时，都要更新对应的索引文件。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">DiskQueueBlock</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String BLOCK_FILE_SUFFIX = <span class="string">".blk"</span>; <span class="comment">// 数据文件</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> BLOCK_SIZE = <span class="number">32</span> * <span class="number">1024</span> * <span class="number">1024</span>; <span class="comment">// 32MB</span></span><br><span class="line"> </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> EOF = -<span class="number">1</span>;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">private</span> String blockFilePath;</span><br><span class="line">    <span class="keyword">private</span> DiskQueueIndex index;</span><br><span class="line">    <span class="keyword">private</span> RandomAccessFile blockFile;</span><br><span class="line">    <span class="keyword">private</span> FileChannel fileChannel;</span><br><span class="line">    <span class="keyword">private</span> ByteBuffer byteBuffer;</span><br><span class="line">    <span class="keyword">private</span> MappedByteBuffer mappedBlock;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">DiskQueueBlock</span><span class="params">(String blockFilePath, DiskQueueIndex index, RandomAccessFile blockFile, FileChannel fileChannel,</span><br><span class="line">                        ByteBuffer byteBuffer, MappedByteBuffer mappedBlock)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.blockFilePath = blockFilePath;</span><br><span class="line">        <span class="keyword">this</span>.index = index;</span><br><span class="line">        <span class="keyword">this</span>.blockFile = blockFile;</span><br><span class="line">        <span class="keyword">this</span>.fileChannel = fileChannel;</span><br><span class="line">        <span class="keyword">this</span>.byteBuffer = byteBuffer;</span><br><span class="line">        <span class="keyword">this</span>.mappedBlock = mappedBlock;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">DiskQueueBlock</span><span class="params">(DiskQueueIndex index, String blockFilePath)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.index = index;</span><br><span class="line">        <span class="keyword">this</span>.blockFilePath = blockFilePath;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            File file = <span class="keyword">new</span> File(blockFilePath);</span><br><span class="line">            <span class="keyword">this</span>.blockFile = <span class="keyword">new</span> RandomAccessFile(file, <span class="string">"rw"</span>);</span><br><span class="line">            <span class="keyword">this</span>.fileChannel = blockFile.getChannel();</span><br><span class="line">            <span class="keyword">this</span>.mappedBlock = fileChannel.map(FileChannel.MapMode.READ_WRITE, <span class="number">0</span>, BLOCK_SIZE);</span><br><span class="line">            <span class="keyword">this</span>.byteBuffer = mappedBlock.load();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> DiskQueueBlock <span class="title">duplicate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> DiskQueueBlock(<span class="keyword">this</span>.blockFilePath, <span class="keyword">this</span>.index, <span class="keyword">this</span>.blockFile, <span class="keyword">this</span>.fileChannel,</span><br><span class="line">                <span class="keyword">this</span>.byteBuffer.duplicate(), <span class="keyword">this</span>.mappedBlock);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">formatBlockFilePath</span><span class="params">(String queueName, <span class="keyword">int</span> fileNum, String fileBackupDir)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> fileBackupDir + File.separator + String.format(<span class="string">"%s_%d%s"</span>, queueName, fileNum, BLOCK_FILE_SUFFIX);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getBlockFilePath</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> blockFilePath;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">putEOF</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.byteBuffer.position(index.getWritePosition());</span><br><span class="line">        <span class="keyword">this</span>.byteBuffer.putInt(EOF);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isSpaceAvailable</span><span class="params">(<span class="keyword">int</span> len)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> increment = len + <span class="number">4</span>;</span><br><span class="line">        <span class="keyword">int</span> writePosition = index.getWritePosition();</span><br><span class="line">        <span class="keyword">return</span> BLOCK_SIZE &gt;= increment + writePosition + <span class="number">4</span>; <span class="comment">// 保证最后有4字节的空间可以写入EOF</span></span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">eof</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> readPosition = index.getReadPosition();</span><br><span class="line">        <span class="keyword">return</span> readPosition &gt; <span class="number">0</span> &amp;&amp; byteBuffer.getInt(readPosition) == EOF;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">write</span><span class="params">(<span class="keyword">byte</span>[] bytes)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> len = bytes.length;</span><br><span class="line">        <span class="keyword">int</span> increment = len + <span class="number">4</span>;</span><br><span class="line">        <span class="keyword">int</span> writePosition = index.getWritePosition();</span><br><span class="line">        byteBuffer.position(writePosition);</span><br><span class="line">        byteBuffer.putInt(len);</span><br><span class="line">        byteBuffer.put(bytes);</span><br><span class="line">        index.putWritePosition(increment + writePosition);</span><br><span class="line">        index.putWriteCounter(index.getWriteCounter() + <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">return</span> increment;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">write</span><span class="params">(<span class="keyword">byte</span>[] bytes, <span class="keyword">int</span> start, <span class="keyword">int</span> len)</span> </span>&#123; </span><br><span class="line">        <span class="keyword">int</span> increment = len + <span class="number">4</span>;</span><br><span class="line">        <span class="keyword">int</span> writePosition = index.getWritePosition();</span><br><span class="line">        byteBuffer.position(writePosition);</span><br><span class="line">        byteBuffer.putInt(len); </span><br><span class="line">        byteBuffer.put(bytes, start, len);</span><br><span class="line">        index.putWritePosition(increment + writePosition);</span><br><span class="line">        index.putWriteCounter(index.getWriteCounter() + <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">return</span> increment;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">byte</span>[] read() &#123;</span><br><span class="line">        <span class="keyword">byte</span>[] bytes;</span><br><span class="line">        <span class="keyword">int</span> readNum = index.getReadNum();</span><br><span class="line">        <span class="keyword">int</span> readPosition = index.getReadPosition();</span><br><span class="line">        <span class="keyword">int</span> writeNum = index.getWriteNum();</span><br><span class="line">        <span class="keyword">int</span> writePosition = index.getWritePosition();</span><br><span class="line">        <span class="keyword">if</span> (readNum == writeNum &amp;&amp; readPosition &gt;= writePosition) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        byteBuffer.position(readPosition);</span><br><span class="line">        <span class="keyword">int</span> dataLength = byteBuffer.getInt();</span><br><span class="line">        <span class="keyword">if</span> (dataLength &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[dataLength];</span><br><span class="line">        byteBuffer.get(bytes);</span><br><span class="line">        index.putReadPosition(readPosition + bytes.length + <span class="number">4</span>);</span><br><span class="line">        index.putReadCounter(index.getReadCounter() + <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">return</span> bytes;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sync</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mappedBlock != <span class="keyword">null</span>) &#123;</span><br><span class="line">            mappedBlock.force();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">close</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (mappedBlock == <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            sync();</span><br><span class="line">            AccessController.doPrivileged(<span class="keyword">new</span> PrivilegedAction&lt;Object&gt;() &#123;</span><br><span class="line">                <span class="annotation">@SuppressWarnings</span>(<span class="string">"restriction"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        Method getCleanerMethod = mappedBlock.getClass().getMethod(<span class="string">"cleaner"</span>);</span><br><span class="line">                        getCleanerMethod.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">                        sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(mappedBlock);</span><br><span class="line">                        cleaner.clean();</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                        log.error(<span class="string">"close fqueue block file failed"</span>, e);</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">            mappedBlock = <span class="keyword">null</span>;</span><br><span class="line">            byteBuffer = <span class="keyword">null</span>;</span><br><span class="line">            fileChannel.close();</span><br><span class="line">            blockFile.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            log.error(<span class="string">"close fqueue block file failed"</span>, e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="DiskQueue"><a href="#DiskQueue" class="headerlink" title="DiskQueue"></a>DiskQueue</h2><p>　　接着我们看看另一个内部类DiskQueue，可以将它理解为一个代理类，所有对索引文件和数据文件的操作，都是通过该类提供的方法完成的。初始化该对象时，需要先判断要读取的数据文件和要写入的数据文件是不是同一个数据文件，作者同样实现了数据文件的读写分离。<br>　　这里，作者还提供了两个ReentrantLock类型的读锁和写锁。进行相应的操作时都要加锁，此处会不会造成性能的问题暂时不做测试分析。在进行读写时，都要判断是否需要进入下一个数据文件进行操作。<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br></pre></td><td class="code"><pre><span class="line">public static class DiskQueue extends AbstractQueue&lt;byte[]&gt; &#123;</span><br><span class="line"></span><br><span class="line">    private String queueName;</span><br><span class="line">    private String fileBackupDir;</span><br><span class="line">    private DiskQueueIndex index;</span><br><span class="line">    private DiskQueueBlock readBlock;</span><br><span class="line">    private DiskQueueBlock writeBlock;</span><br><span class="line">    private ReentrantLock readLock;</span><br><span class="line">    private ReentrantLock writeLock;</span><br><span class="line">    private AtomicInteger size;</span><br><span class="line"></span><br><span class="line">    public DiskQueue(String queueName, String fileBackupDir) &#123;</span><br><span class="line">        this.queueName = queueName;</span><br><span class="line">        this.fileBackupDir = fileBackupDir;</span><br><span class="line">        this.readLock = new ReentrantLock();</span><br><span class="line">        this.writeLock = new ReentrantLock();</span><br><span class="line">        this.index = new DiskQueueIndex(DiskQueueIndex.formatIndexFilePath(queueName, fileBackupDir));</span><br><span class="line">        this.size = new AtomicInteger(index.getWriteCounter() - index.getReadCounter());</span><br><span class="line">        this.writeBlock = new DiskQueueBlock(index, DiskQueueBlock.formatBlockFilePath(queueName,</span><br><span class="line">                index.getWriteNum(), fileBackupDir));</span><br><span class="line">        if (index.getReadNum() == index.getWriteNum()) &#123;</span><br><span class="line">            this.readBlock = this.writeBlock.duplicate();</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            this.readBlock = new DiskQueueBlock(index, DiskQueueBlock.formatBlockFilePath(queueName,</span><br><span class="line">                    index.getReadNum(), fileBackupDir));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    public int getFlag()&#123;</span><br><span class="line">    	return index.getFlag();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    public void setFlag(int flag)&#123;</span><br><span class="line">    	index.putFlag(flag);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public Iterator&lt;byte[]&gt; iterator() &#123;</span><br><span class="line">        throw new UnsupportedOperationException();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public int size() &#123;</span><br><span class="line">        return this.size.get();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void rotateNextWriteBlock() &#123;</span><br><span class="line">        int nextWriteBlockNum = index.getWriteNum() + 1;</span><br><span class="line">        nextWriteBlockNum = (nextWriteBlockNum &lt; 0) ? 0 : nextWriteBlockNum;</span><br><span class="line">        writeBlock.putEOF();</span><br><span class="line">        if (index.getReadNum() == index.getWriteNum()) &#123;</span><br><span class="line">            writeBlock.sync();</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            writeBlock.close();</span><br><span class="line">        &#125;</span><br><span class="line">        writeBlock = new DiskQueueBlock(index, DiskQueueBlock.formatBlockFilePath(queueName,</span><br><span class="line">                nextWriteBlockNum, fileBackupDir));</span><br><span class="line">        index.putWriteNum(nextWriteBlockNum);</span><br><span class="line">        index.putWritePosition(0);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public boolean offer(byte[] bytes) &#123;</span><br><span class="line">        if (bytes == null || bytes.length == 0) &#123;</span><br><span class="line">            return true;</span><br><span class="line">        &#125;</span><br><span class="line">        writeLock.lock();</span><br><span class="line">        try &#123;</span><br><span class="line">            if (!writeBlock.isSpaceAvailable(bytes.length)) &#123;</span><br><span class="line">                rotateNextWriteBlock();</span><br><span class="line">            &#125;</span><br><span class="line">            writeBlock.write(bytes);</span><br><span class="line">            size.incrementAndGet();</span><br><span class="line">            return true;</span><br><span class="line">        &#125; finally &#123;</span><br><span class="line">            writeLock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void rotateNextReadBlock() &#123;</span><br><span class="line">        if (index.getReadNum() == index.getWriteNum()) &#123;</span><br><span class="line">            // 读缓存块的滑动必须发生在写缓存块滑动之后</span><br><span class="line">            return;</span><br><span class="line">        &#125;</span><br><span class="line">        int nextReadBlockNum = index.getReadNum() + 1;</span><br><span class="line">        nextReadBlockNum = (nextReadBlockNum &lt; 0) ? 0 : nextReadBlockNum;</span><br><span class="line">        readBlock.close();</span><br><span class="line">        String blockPath = readBlock.getBlockFilePath();</span><br><span class="line">        if (nextReadBlockNum == index.getWriteNum()) &#123;</span><br><span class="line">            readBlock = writeBlock.duplicate();</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            readBlock = new DiskQueueBlock(index, DiskQueueBlock.formatBlockFilePath(queueName,</span><br><span class="line">                    nextReadBlockNum, fileBackupDir));</span><br><span class="line">        &#125;</span><br><span class="line">        index.putReadNum(nextReadBlockNum);</span><br><span class="line">        index.putReadPosition(0);</span><br><span class="line">        DiskQueuePool.toClear(blockPath);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public byte[] poll() &#123;</span><br><span class="line">        readLock.lock();</span><br><span class="line">        try &#123;</span><br><span class="line">            if (readBlock.eof()) &#123;</span><br><span class="line">                rotateNextReadBlock();</span><br><span class="line">            &#125;</span><br><span class="line">            byte[] bytes = readBlock.read();</span><br><span class="line">            if (bytes != null) &#123;</span><br><span class="line">                size.decrementAndGet();</span><br><span class="line">            &#125;</span><br><span class="line">            return bytes;</span><br><span class="line">        &#125; finally &#123;</span><br><span class="line">            readLock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public byte[] peek() &#123;</span><br><span class="line">        throw new UnsupportedOperationException();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void sync() &#123;</span><br><span class="line">        index.sync();</span><br><span class="line">        // read block只读，不用同步</span><br><span class="line">        writeBlock.sync();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void close() &#123;</span><br><span class="line">        writeBlock.close();</span><br><span class="line">        if (index.getReadNum() != index.getWriteNum()) &#123;</span><br><span class="line">            readBlock.close();</span><br><span class="line">        &#125;</span><br><span class="line">        index.reset();</span><br><span class="line">        index.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="DiskQueuePool"><a href="#DiskQueuePool" class="headerlink" title="DiskQueuePool"></a>DiskQueuePool</h2><p>　　最后我们看看线程池的实现机制。初始化该类时，都会先调用scanDir方法将离线的队列文件加载，每个队列都会单独对应一个的索引文件和多个数据文件。之后会初始化一个ScheduledExecutorService类型的定时任务，每隔10秒钟，调用索引文件和数据文件的mappedbytebuffer的force，将数据刷入磁盘！<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> BlockingQueue&lt;String&gt; deletingQueue = <span class="keyword">new</span> LinkedBlockingQueue&lt;String&gt;();</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> DiskQueuePool instance = <span class="keyword">null</span>;</span><br><span class="line"><span class="keyword">private</span> String fileBackupPath;</span><br><span class="line"><span class="keyword">private</span> Map&lt;String, DiskQueue&gt; queueMap;</span><br><span class="line"><span class="keyword">private</span> ScheduledExecutorService syncService;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="title">DiskQueuePool</span><span class="params">(String fileBackupPath)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.fileBackupPath = fileBackupPath;</span><br><span class="line">    File fileBackupDir = <span class="keyword">new</span> File(fileBackupPath);</span><br><span class="line">    <span class="keyword">if</span> (!fileBackupDir.exists() &amp;&amp; !fileBackupDir.mkdir()) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"can not create directory"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">this</span>.queueMap = scanDir(fileBackupDir);</span><br><span class="line">    <span class="keyword">this</span>.syncService = Executors.newSingleThreadScheduledExecutor();</span><br><span class="line">    <span class="keyword">this</span>.syncService.scheduleAtFixedRate(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">        <span class="annotation">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">for</span> (DiskQueue q : queueMap.values()) &#123;</span><br><span class="line">                q.sync();</span><br><span class="line">            &#125;</span><br><span class="line">            deleteBlockFile();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;, <span class="number">1000</span>, <span class="number">10000</span>, TimeUnit.MILLISECONDS);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Map&lt;String, DiskQueue&gt; getQueryMap()&#123;</span><br><span class="line">	<span class="keyword">return</span> instance.queueMap;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">deleteBlockFile</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    String blockFilePath = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">while</span>( (blockFilePath = deletingQueue.poll()) != <span class="keyword">null</span>)&#123;</span><br><span class="line">    	blockFilePath = blockFilePath.trim();</span><br><span class="line">    	<span class="keyword">if</span>(blockFilePath.equals(<span class="string">""</span>)) <span class="keyword">continue</span>;</span><br><span class="line">    	log.info(<span class="string">"Delete File[%s]"</span>, blockFilePath);   </span><br><span class="line">        File delFile = <span class="keyword">new</span> File(blockFilePath);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (!delFile.delete()) &#123;</span><br><span class="line">                log.warn(<span class="string">"block file:%s delete failed"</span>, blockFilePath);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SecurityException e) &#123;</span><br><span class="line">            log.error(<span class="string">"security manager exists, delete denied"</span>);</span><br><span class="line">        &#125; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">toClear</span><span class="params">(String filePath)</span> </span>&#123;</span><br><span class="line">    deletingQueue.add(filePath);</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">private</span> Map&lt;String, DiskQueue&gt; scanDir(File fileBackupDir) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!fileBackupDir.isDirectory()) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"it is not a directory"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    Map&lt;String, DiskQueue&gt; queues = <span class="keyword">new</span> HashMap&lt;String, DiskQueue&gt;();</span><br><span class="line">    File[] indexFiles = fileBackupDir.listFiles(<span class="keyword">new</span> FilenameFilter() &#123;</span><br><span class="line">        <span class="annotation">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">accept</span><span class="params">(File dir, String name)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> DiskQueueIndex.isIndexFile(name);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">if</span> (indexFiles != <span class="keyword">null</span> &amp;&amp; indexFiles.length&gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (File indexFile : indexFiles) &#123;</span><br><span class="line">            String queueName = DiskQueueIndex.parseQueueName(indexFile.getName());</span><br><span class="line">            queues.put(queueName, <span class="keyword">new</span> DiskQueue(queueName, fileBackupPath));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> queues;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">(String deployPath)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (instance == <span class="keyword">null</span>) &#123;</span><br><span class="line">        instance = <span class="keyword">new</span> DiskQueuePool(deployPath);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">dispose</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.syncService.shutdown();</span><br><span class="line">    <span class="keyword">for</span> (DiskQueue q : queueMap.values()) &#123;</span><br><span class="line">        q.close();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span> (!deletingQueue.isEmpty()) &#123;</span><br><span class="line">        deleteBlockFile();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">destory</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (instance != <span class="keyword">null</span>) &#123;</span><br><span class="line">        instance.dispose();</span><br><span class="line">        instance = <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">private</span> DiskQueue <span class="title">getQueueFromPool</span><span class="params">(String queueName)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (queueMap.containsKey(queueName)) &#123;</span><br><span class="line">        <span class="keyword">return</span> queueMap.get(queueName);</span><br><span class="line">    &#125;</span><br><span class="line">    DiskQueue q = <span class="keyword">new</span> DiskQueue(queueName, fileBackupPath);</span><br><span class="line">    queueMap.put(queueName, q);</span><br><span class="line">    <span class="keyword">return</span> q;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">static</span> DiskQueue <span class="title">getDiskQueue</span><span class="params">(String queueName)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(instance == <span class="keyword">null</span>)&#123;</span><br><span class="line">		<span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">"call DiskQueuePool.init(dir) first"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="keyword">if</span> (queueName==<span class="keyword">null</span> || queueName.trim().equals(<span class="string">""</span>)) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"empty queue name"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> instance.getQueueFromPool(queueName);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="u603B_u7ED3"><a href="#u603B_u7ED3" class="headerlink" title="总结"></a>总结</h2><p>　　为保证消息持久化的性能，作者主要采取了以下几种机制：<br>　　1. 采用MappedByteBuffer实现高效的文件读写<br>　　2. 采用读写分离的机制实现磁盘的顺序读写，保证效率<br>　　3. 对数据文件分块，每块32M，方便迁移和维护<br>　　4. 每个队列独立存储，包含一个索引文件和多个数据文件，避免队列之间的相互影响</p>
<h2 id="u53C2_u8003_u94FE_u63A5"><a href="#u53C2_u8003_u94FE_u63A5" class="headerlink" title="参考链接"></a>参考链接</h2><p><a href="http://my.oschina.net/xnkl/blog/477690?fromerr=Gr7v5nwR" target="_blank" rel="external">http://my.oschina.net/xnkl/blog/477690?fromerr=Gr7v5nwR</a><br><a href="http://git.oschina.net/rushmore/zbus" target="_blank" rel="external">http://git.oschina.net/rushmore/zbus</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2016/01/15/java/11.zbus消息存储机制/" data-id="cijzzpvem00dx1cfsx1hkessv" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/zbus/">zbus</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/01/17/netty/1.Netty系列之简介/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Netty系列一之简介
        
      </div>
    </a>
  
  
    <a href="/2016/01/14/java/12.分布式事物/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">分布式事务</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/cache/">cache</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/database/">database</a><span class="category-list-count">13</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/docker/">docker</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gis/">gis</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/git/">git</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/go/">go</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gradle/">gradle</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/groovy/">groovy</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/hadoop/">hadoop</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/java/">java</a><span class="category-list-count">18</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/linux/">linux</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/log/">log</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mongodb/">mongodb</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mq/">mq</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mybatis/">mybatis</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/netty/">netty</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nginx/">nginx</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nio/">nio</a><span class="category-list-count">19</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nodejs/">nodejs</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/redis/">redis</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/rpc/">rpc</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/spring/">spring</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/tomcat/">tomcat</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/zookeeper/">zookeeper</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/杂项/">杂项</a><span class="category-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/aop/">aop</a><span class="tag-list-count">8</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/aspectj/">aspectj</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cache/">cache</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/classloader/">classloader</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/common-loggings/">common-loggings</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/commonjs/">commonjs</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/database/">database</a><span class="tag-list-count">10</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/dubbo/">dubbo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fastdfs/">fastdfs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gclib/">gclib</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/geohash/">geohash</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gis/">gis</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/go/">go</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gradle/">gradle</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/groovy/">groovy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hadoop/">hadoop</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hessisan/">hessisan</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a><span class="tag-list-count">48</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/javassist/">javassist</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/kafka/">kafka</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log/">log</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log4j/">log4j</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/logback/">logback</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/map/">map</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/memecached/">memecached</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mina/">mina</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mongodb/">mongodb</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mq/">mq</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mqtt/">mqtt</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mybatis/">mybatis</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/netty/">netty</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nginx/">nginx</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nio/">nio</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nodejs/">nodejs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/postgresql/">postgresql</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/">protobuf</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/proxy/">proxy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/push/">push</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rabbitmq/">rabbitmq</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/">redis</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rpc/">rpc</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sitemesh/">sitemesh</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sld/">sld</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/slf4j/">slf4j</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/socket/">socket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spatialite-for-android/">spatialite for android</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spi/">spi</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spring/">spring</a><span class="tag-list-count">13</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tomcat/">tomcat</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/transaction/">transaction</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/web/">web</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/xSocket/">xSocket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zbus/">zbus</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zookeeper/">zookeeper</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/数据结构/">数据结构</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/aop/" style="font-size: 15.38px;">aop</a> <a href="/tags/aspectj/" style="font-size: 11.54px;">aspectj</a> <a href="/tags/cache/" style="font-size: 11.54px;">cache</a> <a href="/tags/classloader/" style="font-size: 10px;">classloader</a> <a href="/tags/common-loggings/" style="font-size: 10px;">common-loggings</a> <a href="/tags/commonjs/" style="font-size: 12.31px;">commonjs</a> <a href="/tags/database/" style="font-size: 16.92px;">database</a> <a href="/tags/docker/" style="font-size: 13.85px;">docker</a> <a href="/tags/dubbo/" style="font-size: 10px;">dubbo</a> <a href="/tags/fastdfs/" style="font-size: 10px;">fastdfs</a> <a href="/tags/gclib/" style="font-size: 10.77px;">gclib</a> <a href="/tags/geohash/" style="font-size: 10px;">geohash</a> <a href="/tags/gis/" style="font-size: 10px;">gis</a> <a href="/tags/git/" style="font-size: 10.77px;">git</a> <a href="/tags/go/" style="font-size: 10px;">go</a> <a href="/tags/gradle/" style="font-size: 10px;">gradle</a> <a href="/tags/groovy/" style="font-size: 10px;">groovy</a> <a href="/tags/hadoop/" style="font-size: 10px;">hadoop</a> <a href="/tags/hessisan/" style="font-size: 11.54px;">hessisan</a> <a href="/tags/java/" style="font-size: 20px;">java</a> <a href="/tags/javassist/" style="font-size: 10px;">javassist</a> <a href="/tags/kafka/" style="font-size: 11.54px;">kafka</a> <a href="/tags/linux/" style="font-size: 12.31px;">linux</a> <a href="/tags/log/" style="font-size: 17.69px;">log</a> <a href="/tags/log4j/" style="font-size: 12.31px;">log4j</a> <a href="/tags/logback/" style="font-size: 13.08px;">logback</a> <a href="/tags/map/" style="font-size: 10px;">map</a> <a href="/tags/memecached/" style="font-size: 10.77px;">memecached</a> <a href="/tags/mina/" style="font-size: 13.08px;">mina</a> <a href="/tags/mongodb/" style="font-size: 19.23px;">mongodb</a> <a href="/tags/mq/" style="font-size: 13.85px;">mq</a> <a href="/tags/mqtt/" style="font-size: 10.77px;">mqtt</a> <a href="/tags/mybatis/" style="font-size: 16.15px;">mybatis</a> <a href="/tags/netty/" style="font-size: 10px;">netty</a> <a href="/tags/nginx/" style="font-size: 11.54px;">nginx</a> <a href="/tags/nio/" style="font-size: 13.08px;">nio</a> <a href="/tags/nodejs/" style="font-size: 10px;">nodejs</a> <a href="/tags/postgresql/" style="font-size: 10.77px;">postgresql</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/proxy/" style="font-size: 10px;">proxy</a> <a href="/tags/push/" style="font-size: 10px;">push</a> <a href="/tags/rabbitmq/" style="font-size: 11.54px;">rabbitmq</a> <a href="/tags/redis/" style="font-size: 13.85px;">redis</a> <a href="/tags/rpc/" style="font-size: 19.23px;">rpc</a> <a href="/tags/sitemesh/" style="font-size: 10px;">sitemesh</a> <a href="/tags/sld/" style="font-size: 10px;">sld</a> <a href="/tags/slf4j/" style="font-size: 10px;">slf4j</a> <a href="/tags/socket/" style="font-size: 14.62px;">socket</a> <a href="/tags/spatialite-for-android/" style="font-size: 10px;">spatialite for android</a> <a href="/tags/spi/" style="font-size: 10px;">spi</a> <a href="/tags/spring/" style="font-size: 18.46px;">spring</a> <a href="/tags/tomcat/" style="font-size: 10.77px;">tomcat</a> <a href="/tags/transaction/" style="font-size: 10px;">transaction</a> <a href="/tags/web/" style="font-size: 10px;">web</a> <a href="/tags/xSocket/" style="font-size: 14.62px;">xSocket</a> <a href="/tags/zbus/" style="font-size: 10px;">zbus</a> <a href="/tags/zookeeper/" style="font-size: 10.77px;">zookeeper</a> <a href="/tags/数据结构/" style="font-size: 10.77px;">数据结构</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">January 2016</a><span class="archive-list-count">67</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">December 2015</a><span class="archive-list-count">81</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/11/">November 2015</a><span class="archive-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recents</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2016/01/30/资料/">(no title)</a>
          </li>
        
          <li>
            <a href="/2016/01/30/java/13.Classloader系列一/">ClassLoader系列一</a>
          </li>
        
          <li>
            <a href="/2016/01/29/hadoop/1.hadoop系列之源码编译/">hadoop系列一之源码编译</a>
          </li>
        
          <li>
            <a href="/2016/01/29/go/1.GO系列之入门/">Go系列一之入门</a>
          </li>
        
          <li>
            <a href="/2016/01/28/tomcat/2.tomcat性能优化/">tomcat性能优化</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2016 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

  </div>
</body>
</html>